package com.oocl.o2o.admin.controller;

import java.io.File;
import java.io.FileInputStream;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.List;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;
import javax.ws.rs.GET;

import org.apache.activemq.filter.function.regexMatchFunction;
import org.apache.log4j.Logger;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PatchMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.fasterxml.jackson.annotation.JsonCreator.Mode;
import com.oocl.o2o.admin.entity.FailReason;
import com.oocl.o2o.admin.entity.Merchant;
import com.oocl.o2o.admin.listener.MerchantRegisterJMSListener;
import com.oocl.o2o.admin.service.MerchantService;
import com.oocl.o2o.admin.util.ImageUtil;
import com.oocl.o2o.admin.vo.MerchantVO;

@Controller
@RequestMapping("/sec/merchant")
public class MerchantController {

	@Resource(name = "merchantServiceImpl")
	private MerchantService merchantService;

	private static Logger logger = Logger
			.getLogger(AdvertisementController.class);

	/**
	 * 
	 * @param model
	 * @return
	 */
	@GetMapping
	public String findAllMerchant(Model model) {
		List<Merchant> merchantAllList = merchantService.findAll();
		model.addAttribute("merchantAllList", merchantAllList);
		logger.info("查询所有商家信息！");
		return "sec/showAllList";
	}

	/**
	 * 
	 * @param status
	 * @param model
	 * @return
	 */
	@GetMapping("/status/{status}")
	public String findMerchantlistWithStatus(
			@PathVariable("status") String status, Model model) {
		List<Merchant> merchantListWithStatus = merchantService
				.findByStatus(Integer.parseInt(status));
		model.addAttribute("merchantListWithStatus", merchantListWithStatus);
		return "sec/showByStatusList";
	}

	/**
	 * 拉黑拉白审核通过 同步
	 * 
	 * @param id
	 * @param status
	 * @return
	 */
	@ResponseBody
	@GetMapping("/id/{id}/changeStatus/{status}")
	public String changeStatus(@PathVariable("id") String id,
			@PathVariable("status") String status) {
		Merchant merchant = new Merchant();
		merchant.setId(id);
		merchant.setStatus(Integer.parseInt(status));
		merchantService.changeSta(id, status);
		return "success";
	}

	@ResponseBody
	@PatchMapping("/cstatus/{status}")
	public String changeStatus(@PathVariable("status") String status,
			@RequestBody MerchantVO merchantVO) {
		System.out.println(merchantVO.getReasonContent());
		merchantService.changeStatus(merchantVO);
		return "success";
	}

	@PostMapping("/id/{id}/status/3")
	public String changeStatusToThree(@PathVariable("id") String id,
			String failReason) {
		Merchant merchant = merchantService.changeStatusToThree(id, "3",
				failReason);
		return "redirect:sec/merchant/status/3";
	}

	@ResponseBody
	@GetMapping("/notPassCount")
	public int getNotPassMerchant() {
		List<Merchant> list = merchantService.findByStatus(0);
		if (list == null) {
			return 0;
		} else {
			return list.size();
		}
	}
	/**
	 * 异步
	 * 
	 * @param id
	 * @param type
	 * @param outputStream
	 * @throws Exception
	 */
	@ResponseBody
	@GetMapping("/id/{id}")
	public MerchantVO getMerchantDetail(@PathVariable("id") String id) {
		MerchantVO merchantVO = merchantService.findByIdForDetail(id);
		return merchantVO;
	}

	@GetMapping("/id/{id}/type/{type}")
	public void showImg(@PathVariable("id") String id,
			@PathVariable("type") String type, OutputStream outputStream)
			throws Exception {

		String filePath = merchantService.findByIdForShowImage(id, type);
		ImageUtil.showImage(filePath, outputStream);
	}

	@ResponseBody
	@GetMapping("/count")
	public int getRegisterCount(Model model) {
		int count = MerchantRegisterJMSListener.getRegisterCount();
		MerchantRegisterJMSListener.setRegisterCount(0);
		return count;
	}

	@ResponseBody
	@GetMapping("/merchantCount")
	public int getMerchantCount() {
		List<Merchant> list = merchantService.findAll();
		return list.size();
	}



	@ResponseBody
	@GetMapping("/blackCount")
	public int getNotBlackMerchant() {
		List<Merchant> list = merchantService.findByStatus(2);
		if (list == null) {
			return 0;
		} else {
			return list.size();
		}
	}

}
